package com.album.utils;

import java.lang.ref.SoftReference;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import com.album.R;
import com.album.common.Constants;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
 

/**
 * @author Devin.Hu
 * @date 2011-6-27
 * @version V1.0
 * @description AndroidUtils工具类
 */
public class AndroidUtils {
	 public static Bitmap drawableBitmap(Drawable drawable) {  
	        try{
		        Bitmap bitmap = Bitmap  
		                        .createBitmap(  
		                                        drawable.getIntrinsicWidth(),  
		                                        drawable.getIntrinsicHeight(),  
		                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
		                                                        : Bitmap.Config.RGB_565);  
		        Canvas canvas = new Canvas(bitmap);  
		        //canvas.setBitmap(bitmap);  
		        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
		        drawable.draw(canvas);
		        return bitmap;
	        } catch (Exception e) {
	        	return null;
	        }	          
	    } 
	 public static SoftReference<Bitmap> drawableToBitmap(Drawable drawable) {  
	        
	        Bitmap bitmap = Bitmap  
	                        .createBitmap(  
	                                        drawable.getIntrinsicWidth(),  
	                                        drawable.getIntrinsicHeight(),  
	                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
	                                                        : Bitmap.Config.RGB_565);  
	        Canvas canvas = new Canvas(bitmap);  
	        //canvas.setBitmap(bitmap);  
	        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
	        drawable.draw(canvas);  
	        return new SoftReference<Bitmap>(bitmap);  
	    }  
	    
	/**
	 * 判断网络是否畅通
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} 
		else 
		{
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) 
			{
				for (int i = 0; i < info.length; i++) 
				{
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 判断是否有SD卡
	 * @return
	 */
	public static boolean avaiableSDCard(){  
		
        String status = Environment.getExternalStorageState();  
          
        if(status.equals(Environment.MEDIA_MOUNTED)){  
            return true;  
        }  
        else {  
            return false;  
        }  
    }
	
	/**
	 * 得到本地IP地址
	 * @return
	 */
	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) 
			{
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) 
				{
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) 
					{
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.i(" AndroidUtils getLocalIpAddress : ", ex.toString());
		}
		return null;
	}

	
	/**
	 * 弹出消息提示框
	 * 
	 * @param context
	 * @param msg
	 */
	public static void showMessage(Context context, String msg) {
		Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(context.getResources().getString(R.string.error_title));
		builder.setMessage(msg);
		builder.setCancelable(true);
		builder.create().show();
	}

	/**
	 * 弹出消息提示框
	 * 
	 * @param context
	 * @param msg
	 */
	public static void showMessage(Context context, int msgId) {
		Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(context.getResources().getString(R.string.error_title));
		builder.setMessage(context.getResources().getString(msgId));
		builder.setCancelable(true);
		builder.create().show();
	}

	/**
	 * 弹出消息提示框
	 * 
	 * @param context
	 * @param msg
	 */
	public static void showMessage(String title, Context context, String msg) {
		Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(msg);
		builder.setCancelable(true);
		builder.create().show();
	}
	
	
	/**
	 * 弹出消息提示框
	 * @param context
	 * @param msg
	 * @param gravity
	 */
	public static void ToastMsg(Context context, String msg, int gravity) {
		Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
		toast.setGravity(gravity, 0, 0);
		toast.show();
	}
	
	/**
	 * 弹出消息提示框
	 * @param context
	 * @param msg
	 */
	public static void ToastMsg(Context context,String msg) {
		Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.show();
	}
	 
	/**
	 * 分享功能
	 * @param context
	 * @param shareMsg
	 * @param activityTitle
	 */
	public static void share(Context context,String shareMsg,String activityTitle)
	{
		Intent intent=new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
		intent.putExtra(Intent.EXTRA_TEXT, shareMsg);
		context.startActivity(Intent.createChooser(intent, activityTitle));
	}
	
	public static String getPhoneIMSI(Context context){
		TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
		return (tm.getSubscriberId() == null ? "" : tm.getSubscriberId());
	}
	public static String getPhoneType(Context context){
		TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
		return (tm.getLine1Number() == null ? "" : tm.getLine1Number()); //手机号码
	}
}
